﻿using CharacterCreation.Views.Contracts;
using CharacterCreation.Data.Services;
using CharacterCreation.Data.Models;
using CharacterCreation.Views.ChildWindows;

namespace CharacterCreation.Views {
    /// <summary>
    /// The viewmodel 
    /// </summary>
    public class RaceHostViewModel : BaseHostViewModel {

        RaceService _raceService = RaceService.CreateInstance();

        /// <summary>
        /// Current race
        /// </summary>
        public Race Race {
            get {
                return _raceService.CurrentItem;
            }
        }

        /// <summary>
        /// ctor
        /// </summary>
        public RaceHostViewModel() {
            _raceService.PropertyChanged += (s, e) => {
                if (e.PropertyName == "CurrentItem")
                    RaisePropertyChanged("Race");
            };
        }

        /// <summary>
        /// Add a new item
        /// </summary>
        public override void Add() {
            _raceService.ClearNewItem();
            PopupWindow popupWindow = new PopupWindow(
                new BaseView[] { new RaceDetailView() { IsNewItem = true } }
            );
            popupWindow.Closing += (s, e) => {
                if (popupWindow.DialogResult != null && (bool)popupWindow.DialogResult) {
                    _raceService.SaveNewItem();
                }
            };
            popupWindow.ShowDialog();
        }

        /// <summary>
        /// Delete current item
        /// </summary>
        public override void Delete() {
            _raceService.DeleteCurrentItem();
        }

        /// <summary>
        /// Saves a character
        /// </summary>
        public override void Save() {
            _raceService.SaveCurrentItem();
        }

        /// <summary>
        /// Exports the current races
        /// </summary>
        public override void Export() {
            _raceService.Export();
        }
    }
}
